﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BIZ;
using System.Web.Script.Serialization;
using DomainModel;
using System.Collections;
using System.Text.RegularExpressions;

namespace joyAdmin.Controllers
{
    [DomainConfirmationFilter]
    [BIZ.AdminFilter]
    public class UserInfoController : Controller
    {
        UserOper userOper = new UserOper();
        UserCardOper ucardOper = new UserCardOper();
        //
        // GET: /UserInfo/

        public ActionResult UserReg()
        {
            cmsOper.CheckUserRights("UserReg");
            return View();
        }
        public ActionResult cardlist()
        {
            return View();
        }
  

        public ActionResult readlist()
        {
            return View();
        }
        [ValidateInput(false)]
        public ActionResult saveUserInfo()
        {
            UserInfo _staff = new JavaScriptSerializer().Deserialize<UserInfo>(Request.Form["userinfo"].ToString());
            DateTime dt1 = DateTime.Now;
            DateTime dt2 = (DateTime)_staff.u_birthday;
            _staff.u_age = ((dt1.Year - dt2.Year) + 1);
            if (_staff.u_id != 0)
            {
                _staff.u_updateTime = DateTime.Now;
                _staff.u_lastUser = ((DomainModel.cmsuser)Session["cmsuser"]).id;
                _staff.u_addTime = _staff.u_addTime.ToLocalTime();
                _staff.u_registTime = _staff.u_registTime.ToLocalTime();
            }
            else
            {
                _staff.u_user = ((DomainModel.cmsuser)Session["cmsuser"]).id;
                _staff.u_addTime = DateTime.Now;
                _staff.u_pass = "49BA59ABBE56E057";
            }
            userOper.save(_staff);
            return Json(_staff);
        }
        /// <summary>
        /// 验证用户名和学籍编号是否存在
        /// </summary>
        /// <returns></returns>
        public int reg()
        {
            string nick=Request["nick"]==null?"":Request["nick"].ToString();
            string num=Request["num"]==null?"":Request["num"].ToString();
            string uid = Request["uid"] == null ? "" : Request["uid"].ToString();
            if(nick =="" || num =="" || uid=="")
            {
                return 1;
            }
            else
            {
                return userOper.reg(nick, num,uid);
            }
        }
        public JsonResult getuserinfobyid()
        {
            int uid = Convert.ToInt32(Request.Params["res"]);
            IList list =userOper.getuserinfobyid(uid);
            if (list.Count > 0)
                return Json(list[0]);
            else
                return Json("");
        }
        public JsonResult updatepass()
        {
            int uid = Convert.ToInt32(Request.Params["res"]);
            return Json(userOper.updatepass(uid));
        }

        public JsonResult setcard()
        {
            int uid = Request.Params["res"] == null ? 0 : Convert.ToInt32(Request.Params["res"]);
            string card = Request.Params["card"] == null ? "" : Request.Params["card"].ToString();
            string cmode=Request.Params["cmode"] == null ? "" : Request.Params["cmode"].ToString();
            string ckf = Request.Params["ckf"] == null ? "" : Request.Params["ckf"].ToString();
            string cyj = Request.Params["cyj"] == null ? "" : Request.Params["cyj"].ToString();
            string cbz = Request.Params["cbz"] == null ? "" : Request.Params["cbz"].ToString();
            if (uid == 0 || card == "" || cmode == "" || ckf == "" || cyj =="")
            {
                return Json(0);
            }
            else
            {
                return Json(ucardOper.getcard(uid, card, ((DomainModel.cmsuser)Session["cmsuser"]).id,cmode,ckf,cyj,cbz));
            }
        }
        public JsonResult getcardinfo()
        { 
            string card = Request.Params["card"] == null ? "" : Request.Params["card"].ToString();
            Hashtable tb = new Hashtable();
            tb=ucardOper.getcardinfo(card);
            if (tb == null)
            {
                return Json("");
            }
            else
            {
                return Json(tb["uc_kf"]+","+tb["uc_yj"]+","+tb["uc_bz"]);
            }
        }
        public JsonResult cuvip()
        {
            string uid = Request.Form["res"];
            string uvip = Request.Form["uvip"];
            return Json(userOper.cuvip(uid,uvip));
        }
        public JsonResult cardzx()
        {
            string id = Request.Form["id"];
            string card = Request.Form["card"];
            string fy = Request.Form["fy"];
            string yj = Request.Form["yj"];
            string bz = Request.Form["bz"];
            return Json(ucardOper.updateUserCardState(int.Parse(id), card, fy, yj, bz, ((DomainModel.cmsuser)Session["cmsuser"]).id));
        }

        public JsonResult getUserinfoList()
        {
            int pageno = Convert.ToInt32(Request.Params["pageno"]);
            int pagesize = Convert.ToInt32(Request.Params["pagesize"]);
            string uname = Request.Form["uname"];
            string unick = Request.Form["unick"];
            string unumber = Request.Form["unumber"];
            string ujwclass = Request.Form["ujwclass"];
            string uteacher1 = Request.Form["uteacher1"];
            string ucard = Request.Form["ucard"];
            string uvip = Request.Form["uvip"];
            string ustate = Request.Form["ustate"];
            string uregistTime_s = Request.Form["uregistTime_s"];
            string uregistTime_e = Request.Form["uregistTime_e"];

            string orderName = Request.Form["orderName"];
            string orderEname = Request.Form["orderEname"];
            string orderNumber = Request.Form["orderNumber"];
            string orderBrithday = Request.Form["orderBrithday"];
            string orderVip = Request.Form["orderVip"];
            string orderJwclass = Request.Form["orderJwclass"];
            string orderId = Request.Form["orderId"];
            return Json(userOper.getUserinfoList(uname, unick, unumber, ujwclass, uteacher1, ucard, uvip, ustate, uregistTime_s, uregistTime_e, pageno, pagesize, orderName, orderEname, orderNumber, orderBrithday, orderVip, orderJwclass, orderId));
        }
    }
}
